package com.zdsoft.blp.zongbuju.wode.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/1.
 */
public class ZhuCeActivity extends BaseActivity implements NetCallback {

    private FanHuiView back;
    private Button bt_xiayibu, bt_hqyzm;
    private EditText et_phone, et_yanzhengma;
    private int yellow, hui, green;
    private static final Integer SENDVER = 1;
    private static final Integer VERIFICATION = 2;

    @Override
    protected void init() {
        setContentView(R.layout.zhuce_wode);
        bt_xiayibu = find(R.id.bt_xiayibu);
        bt_hqyzm = find(R.id.bt_hqyzm);
        et_phone = find(R.id.et_phone);
        et_yanzhengma = find(R.id.et_yanzhengma);
        back = find(R.id.back);
        yellow =  getResources().getColor( R.color.yzmyellow);
        hui =  getResources().getColor( R.color.hui);
        green =  getResources().getColor( R.color.green);
        back.getTv_title().setText("注册");
        bt_hqyzm.setEnabled(false);

        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (et_phone.length() == 11) {
                    bt_hqyzm.setBackgroundColor(yellow);
                    bt_hqyzm.setEnabled(true);
                } else {
                    bt_hqyzm.setBackgroundColor(hui);
                    bt_hqyzm.setEnabled(false);
                }

            }
        });
        et_yanzhengma.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (et_yanzhengma.getText().toString().trim().length() != 0) {
                    bt_xiayibu.setBackgroundColor(green);
                } else {
                    bt_xiayibu.setBackgroundColor(hui);
                }
            }
        });

        bt_hqyzm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_phone.getText().toString().trim().length() == 11) {
                    Net net = new Net();
                    net.url = API.MEMBER_URL;
                    net.nameSpace = API.MEMBER_NAMESPACE;
                    net.methodName = API.SENDVER;
                    Map<String, Object> map = new HashMap<>();
                    map.put("mobile", et_phone.getText().toString().trim());
                    net.parms = map;
                    net.code = SENDVER;
                    getServer(net, ZhuCeActivity.this);
                } else {
                    Toast.makeText(getApplicationContext(), "请输入手机号", Toast.LENGTH_SHORT).show();
                }
            }
        });

        bt_xiayibu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_yanzhengma.getText().toString().trim() != "" && et_phone.getText().toString().trim().length() == 11) {
                    Net net = new Net();
                    net.url = API.MEMBER_URL;
                    net.nameSpace = API.MEMBER_NAMESPACE;
                    net.methodName = API.VERIFICATION;
                    Map<String, Object> map = new HashMap<>();
                    map.put("mobile", et_phone.getText().toString().trim());
                    map.put("verCode", et_yanzhengma.getText().toString().trim());
                    net.parms = map;
                    net.code = VERIFICATION;
                    getServer(net, ZhuCeActivity.this);
                }
            }
        });
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag && code == SENDVER) {
            try {
                JSONObject object = new JSONObject(res);
                if (object.getBoolean("result")) {
                    bt_hqyzm.setEnabled(false);
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            try {
                                for (int i = 60; i > 0; i--) {
                                    sleep(1000);
                                    Message msg = handler.obtainMessage();
                                    msg.what = 1;
                                    msg.obj = i;
                                    handler.sendMessage(msg);
                                }
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();
                } else {
                    Toast.makeText(getApplicationContext(), object.getString("msg"), Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        if (flag && code == VERIFICATION) {
            try {
                JSONObject object = new JSONObject(res);
                if (!object.getBoolean("result")) {
                    Toast.makeText(getApplicationContext(), object.getString("msg"), Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(getApplicationContext(), SheZhiZHActivity.class);
                    intent.putExtra("phone",et_phone.getText().toString().trim());
                    startActivity(intent);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                bt_hqyzm.setText(msg.obj + "秒后重试");
                bt_hqyzm.setBackgroundColor(hui);
                if (((int) msg.obj) == 1) {
                    bt_hqyzm.setText("重新获取验证码");
                    bt_hqyzm.setEnabled(true);
                    bt_hqyzm.setBackgroundColor(yellow);
                }
            }
        }
    };
}
